package com.xuexiang.xuidemo.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;

import com.xuexiang.xui_lib.component.flowlayout.FlowTagLayout;
import com.xuexiang.xui_lib.component.flowlayout.FlowTagProvider;
import com.xuexiang.xui_lib.util.LogUtils;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.util.ToastUtil;

import java.util.List;

/**
 * FlexboxLayoutManager + RecyclerView
 * 流标签
 */
public class FlexboxLayoutAbilitySlice extends AbilitySlice  implements Component.ClickedListener {
    private Text text_title,text_title_bottom;
    private Image image_back;
    private FlowTagLayout mNormalFlowTagLayout;
    private FlowTagLayout mSingleFlowTagLayout;
    private FlowTagLayout mSingleCancelableFlowTagLayout;
    private FlowTagLayout mMultiFlowTagLayout;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_flexbox_layout);
        initComponent();
        initTitle();
        inintClickedListener();
        getUITaskDispatcher().asyncDispatch(new Runnable() {
            @Override
            public void run() {
                initmNormalFlowTagLayout();
                initmSingleFlowTagLayout();
                initSingleCancelableFlowTagLayout();
                initMultiFlowTagLayout();
            }
        });
    }

    private void initmNormalFlowTagLayout() {
        FlowTagProvider tagAdapter = new FlowTagProvider(FlexboxLayoutAbilitySlice.this);
        mNormalFlowTagLayout=(FlowTagLayout) findComponentById(ResourceTable.Id_flowlayout_normal_select);
        mNormalFlowTagLayout.setProvider(tagAdapter);
        mNormalFlowTagLayout.setOnTagClickListener(new FlowTagLayout.OnTagClickListener() {
            @Override
            public void onItemClick(FlowTagLayout parent, Component component, int position) {
                ToastUtil.showToast(FlexboxLayoutAbilitySlice.this,"点击了:"+parent.getProvider().getItem(position)+"");
            }
        });
        tagAdapter.addTags(getData());
    }
    private void initmSingleFlowTagLayout() {
        FlowTagProvider tagAdapter = new FlowTagProvider(getContext());
        mSingleFlowTagLayout=(FlowTagLayout) findComponentById(ResourceTable.Id_flowlayout_single_select);
        mSingleFlowTagLayout.setProvider(tagAdapter);
        mSingleFlowTagLayout.setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_SINGLE);
        mSingleFlowTagLayout.setOnTagSelectListener((parent, position, selectedList) -> ToastUtil.showToast(FlexboxLayoutAbilitySlice.this,getSelectedText(parent, selectedList)+""));
        tagAdapter.addTags(getData());
        tagAdapter.setSelectedPositions(2,3,4);
    }
    private void initSingleCancelableFlowTagLayout() {
        FlowTagProvider tagAdapter = new FlowTagProvider(getContext());
        mSingleCancelableFlowTagLayout=(FlowTagLayout) findComponentById(ResourceTable.Id_flowlayout_single_select_cancelable);
        mSingleCancelableFlowTagLayout.setProvider(tagAdapter);
        mSingleCancelableFlowTagLayout.setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_SINGLE);
        mSingleCancelableFlowTagLayout.setOnTagSelectListener((parent, position, selectedList) ->  ToastUtil.showToast(FlexboxLayoutAbilitySlice.this,getSelectedText(parent, selectedList)+""));
        tagAdapter.addTags(getData());
        mSingleCancelableFlowTagLayout.setSelectedPositions(2,3,4);

    }

    private void initMultiFlowTagLayout() {
        FlowTagProvider tagAdapter = new FlowTagProvider(getContext());
        mMultiFlowTagLayout=(FlowTagLayout) findComponentById(ResourceTable.Id_flowlayout_multi_select);
        mMultiFlowTagLayout.setProvider(tagAdapter);
        mMultiFlowTagLayout.setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_MULTI);
        mMultiFlowTagLayout.setOnTagSelectListener((parent, position, selectedList) -> showCustomText(FlexboxLayoutAbilitySlice.this,getSelectedText(parent, selectedList)+""));
        tagAdapter.addTags(getData());
        tagAdapter.setSelectedPositions(2, 3, 4);


    }

    public static void showCustomText(Context context, String toastStr){
            Text textComponent = new Text(context);
            textComponent.setText(toastStr);
            int padding = AttrHelper.fp2px(10,context);
            //设置间距为10vp
            //int left, int top, int right, int bottom
            textComponent.setPadding(padding, 5, padding, 5);
            textComponent.setTextColor(Color.WHITE);
            textComponent.setTextAlignment(TextAlignment.LEFT);
            textComponent.setTextSize(AttrHelper.fp2px(12,context));
            ShapeElement shapeElement = new ShapeElement();
          //  shapeElement.setShape(ShapeElement.RECTANGLE);
            //设置背景半透明
            shapeElement.setRgbColor(RgbColor.fromArgbInt(Color.rgb(108,118,124)));
            shapeElement.setCornerRadius(AttrHelper.fp2px(40, context));
            textComponent.setBackground(shapeElement);
            //设置文字允许多行
            textComponent.setMultipleLine(true);

            DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig();
            layoutConfig.width = ComponentContainer.LayoutConfig.MATCH_CONTENT;
            layoutConfig.height = ComponentContainer.LayoutConfig.MATCH_CONTENT;
            layoutConfig.alignment = LayoutAlignment.CENTER;

            layoutConfig.setMarginBottom(125);
            textComponent.setLayoutConfig(layoutConfig);
            ToastDialog toastDialog =  new ToastDialog(context).setDuration(500).setComponent(textComponent).setAlignment(LayoutAlignment.BOTTOM);
            //设置弹框背景透明
           toastDialog.setTransparent(true);
            toastDialog.show();
    }
    private void initComponent() {
        image_back=(Image) findComponentById(ResourceTable.Id_image_back);
        text_title=(Text)findComponentById(ResourceTable.Id_text_title);
        text_title_bottom=(Text)findComponentById(ResourceTable.Id_text_title_bottom);
    }
    private void inintClickedListener() {
        image_back.setClickedListener(this);
    }

    private void initTitle() {
            text_title.setText("FlexboxLayoutManager + RecyclerView");
            text_title_bottom.setText("流标签");
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()){
            case ResourceTable.Id_image_back:
                onBackPressed();
                break;
        }
    }
    private String getSelectedText(FlowTagLayout parent, List<Integer> selectedList) {
        StringBuilder sb = new StringBuilder("选中的内容:\n");
        for (int index : selectedList) {
            sb.append(parent.getProvider().getItem(index));
            sb.append(";");
        }
        return sb.toString();
    }

    private  String [] getData(){
        String[] tags_values = new String[0];
        try {
            tags_values=getContext().getResourceManager().getElement(ResourceTable.Strarray_tags_values).getStringArray();
        }catch (Exception e){
            LogUtils.info("e=====>",e.getMessage());
          }
        return tags_values;
    };
}
